package com.ly.houses.thread;

import com.alibaba.fastjson.JSON;
import com.ly.houses.model.Building;
import com.ly.houses.model.House;
import com.ly.houses.service.HouseService;
import com.ly.houses.service.HousesMailService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class UpdateHouseDetail implements Runnable {
    HouseService houseService;
    HousesMailService housesMailService;
    Building building;
    CountDownLatch countDown;

    public UpdateHouseDetail(CountDownLatch countDown, Building building, HouseService houseService, HousesMailService housesMailService) {
        this.building = building;
        this.houseService = houseService;
        this.housesMailService = housesMailService;
        this.countDown = countDown;
    }

    @Override
    public void run() {
        List<House> failHouses = new ArrayList<>();
        List<House> removeHouse = new ArrayList<>();
        List<House> isHouses = houseService.listHouseByBuildingId(building.getId());
        List<House> removeHouses = new ArrayList<>();
        String buildsUrl = "http://www.ccfdw.net/ecdomain/lpcs/xmxx/lpbxx_new.jsp?lid=" + building.getPublicId();
        Document initDocument = null;
        try {
            initDocument = Jsoup.connect(buildsUrl).get();
        } catch (IOException e) {
            try {
                initDocument = Jsoup.connect(buildsUrl).get();
            } catch (IOException e1) {
                e1.printStackTrace();
                housesMailService.sendSimpleMailMessage("buildingId为" + building.getId() + "的查询组失败");
                return;
            }
        }
        Elements element = initDocument.select("a");
        List<House> saveHouses = new ArrayList<>();
        List<House> updateHouses = new ArrayList<>();
        for (int i = 1; i < element.size(); i++) {
            House house = new House();
            String publicId = element.get(i).attr("onclick").split("'")[1];
            boolean isHas = false;
            for (House isHouse : isHouses) {
                if (isHouse.getPublicId().equals(publicId)) {
                    isHas = true;
                    removeHouses.add(isHouse);
                    house = isHouse;
                    break;
                }
            }
            String name = element.get(i).text();
            String color = element.get(i).parent().parent().parent().parent().parent().attr("bgcolor");
            String roomStatus = "";
            house.setPublicId(publicId);
            house.setBuildingPublicId(building.getPublicId());
            house.setName(name);
            house.setBuildingId(building.getId());
            if ("#00c112".equals(color)) {
                roomStatus = "可售";
            } else if ("#04fffa".equals(color)) {
                roomStatus = "回迁房";
            } else if ("#4a036e".equals(color)) {
                roomStatus = "已预告抵押";
            } else if ("#9e42cb".equals(color)) {
                roomStatus = "已预告";
            } else if ("#CCFFFF".equals(color)) {
                roomStatus = "正在受理";
            } else if ("#bbbea9".equals(color)) {
                roomStatus = "自留";
            } else if ("#d37887".equals(color)) {
                roomStatus = "已查封、已冻结、已限制";
            } else if ("#d4090d".equals(color)) {
                roomStatus = "已备案";
            } else if ("#f0ef79".equals(color)) {
                roomStatus = "已售";
            } else if ("#fbfdff".equals(color)) {
                roomStatus = "不可售";
            } else if ("#ffc8ff".equals(color)) {
                roomStatus = "已办产权";
            } else if ("#ffedf1".equals(color)) {
                roomStatus = "已抵押";
            }
            house.setStatus(roomStatus);
            String detailUrl = "http://www.ccfdw.net/ecdomain/lpcs/xmxx/huxx.jsp?hid=" + house.getPublicId() + "&&lid=" + house.getBuildingPublicId();
            Document detaile = null;
            try {
                detaile = Jsoup.connect(detailUrl).get();
            } catch (IOException e) {
//                e.printStackTrace();
                try {
//                        Thread.sleep(1000);
                    detaile = Jsoup.connect(detailUrl).get();
                } catch (Exception e1) {
                    failHouses.add(house);
                    e1.printStackTrace();
                    System.out.println(i);
                    continue;
                }
            }
//                System.out.println(detaile.toString());
//                System.out.println(detaile.text());

            if (detaile == null || detaile.text().equals("")) {
                removeHouse.add(house);
                continue;
            }
            if (detaile.select("tbody") == null || detaile.select("tbody").text().equals("")) {
                removeHouse.add(house);
                continue;
            }
            if (detaile.select("tbody").get(0) == null || detaile.select("tbody").get(0).text().equals("")) {
                removeHouse.add(house);
                continue;
            }
            if (detaile.select("tbody").get(0).select("tbody") == null || detaile.select("tbody").get(0).select("tbody").text().equals("")) {
                removeHouse.add(house);
                continue;
            }
//                try {
//                    if (detaile == null || detaile.select("tbody") == null ||detaile.select("tbody").get(0)==null||detaile.select("tbody").get(0).select("tbody") == null) {
//                        fileHouse.add(house);
//                        continue;
//                    }
//                }catch (Exception e){
//                    housesMailService.sendSimpleMailMessage(e.getMessage());
//                    System.out.println(detaile);
//                    System.out.println(detaile.select("tbody"));
//                    System.out.println(detaile.select("tbody").get(0));
//                    System.out.println(detaile.select("tbody").get(0).select("tbody"));
//                    System.out.println(detaile);
//
//                }

            Elements trElements = detaile.select("tbody").get(0).select("tbody").get(0).select("tr");
            String unit = trElements.get(1).select("td").get(1).text();
            house.setUnit(unit);
            String floor = trElements.get(1).select("td").get(5).text();
            house.setFloor(floor);

            String houseType = trElements.get(2).select("td").get(1).text();
            house.setHouseType(houseType);

            String structure = trElements.get(2).select("td").get(3).text();
            house.setStructure(structure);
            String useBy = trElements.get(2).select("td").get(5).text();
            house.setUseBy(useBy);
            String area = trElements.get(3).select("td").get(1).text();
            house.setArea(area);
            String publicArea = trElements.get(3).select("td").get(3).text();
            house.setPublicArea(publicArea);
            String balconyArea = trElements.get(3).select("td").get(5).text();
            house.setBalconyArea(balconyArea);
            String balconyType = trElements.get(4).select("td").get(1).text();
            house.setBalconyType(balconyType);
            String seize = trElements.get(4).select("td").get(5).text();
            house.setSeize(seize);
            String mortgage = trElements.get(5).select("td").get(1).text();
            house.setMortgage(mortgage);
            String price = trElements.get(5).select("td").get(3).text();
            house.setPrice(price);
            String useByFor = trElements.get(5).select("td").get(5).text();
            house.setUseByFor(useByFor);
            String buildingArea = trElements.get(6).select("td").get(1).text();
            house.setBuildingArea(buildingArea);
            String storeyHeight = trElements.get(6).select("td").get(3).text();
            house.setStoreyHeight(storeyHeight);
            if (isHas) {
                updateHouses.add(house);
            } else {
                saveHouses.add(house);
            }
        }
        isHouses.removeAll(removeHouses);
        if (isHouses.size() > 0) {
            houseService.batchDelete(isHouses);
        }
        if (removeHouse.size() > 0) {
            houseService.batchDelete(removeHouse);
        }
        if (saveHouses.size() > 0) {
            houseService.batchSave(saveHouses);
        }
        if (updateHouses.size() > 0) {
            houseService.batchUpdate(updateHouses);
        }
        if (failHouses.size() > 0) {
            housesMailService.sendSimpleMailMessage("failHouses为" + JSON.toJSONString(failHouses));
        }
        countDown.countDown();
//        if(building.getId().equals(708)){
//            System.out.println(System.currentTimeMillis());
//        }

    }
}
